var QQMapWX = require('../libs/qqmap-wx-jssdk.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifLen: false,
    telephone: '',
    password: '',
    code: '',
    verifyCodeTime: '获取验证码',
    timer: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  //手机号前端校验
  onPhoneEvent: function(e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ telephone: e.detail.value.replace(/[^1-9]/g, '') });
    } else {
      that.setData({ telephone: e.detail.value.replace(/\D/g, '') });
    }
    if (e.detail.value.length == 11) {
      wx.request({
        method: 'POST',
        url: app.preUrl + 'queryTelephone',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          telephone: that.data.telephone
        },
        success: function(res) {
          console.log(res);
          if (res.data.code == 206) {
            that.setData({ ifLen: true });
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            });
            that.setData({ ifLen: false });
          }
        },
        fail: function(error) {}
      });
    } else {
      that.setData({ ifLen: false });
    }
  },

  //手机号验证码前端校验
  onCodeEvent: function(e) {
    var that = this;
    that.setData({ code: e.detail.value.replace(/\D/g, '') });
  },
  //密码校验
  onPassword(e) {
    var that = this;
    that.setData({ password: e.detail.value.replace(/(^\s*)|(\s*$)/g, '') });
  },
  //提交手机号获取验证码
  onPhoneNext: function() {
    var that = this;
    var telephone = that.data.telephone;
    var c = 60;
    if (!that.data.ifLen) {
      return;
    }
    if (telephone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      });
      return false;
    }
    if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    that.setData({ buthiddenNext: true });
    that.data.timer = setInterval(function() {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + 's后重发'
      });
      if (c == 0) {
        clearInterval(that.data.timer);
        that.setData({
          verifyCodeTime: '获取验证码',
          buthiddenNext: false
        });
      }
    }, 1000);
    wx.request({
      method: 'POST',
      url: app.preUrl + 'obtainMessage',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        telephone: that.data.telephone
      },
      success: function(res) {
        console.log(res);
        if (res.data.code == 100) {
          console.log('发送成功');
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          });
          clearInterval(that.data.timer);
          that.setData({
            verifyCodeTime: '获取验证码',
            buthiddenNext: false
          });
        }
      },
      fail: function(error) {
        that.setData({ buthiddenNext: false });
      }
    });
  },

  //提交按钮
  bindFormSubmit: function(e) {
    var that = this;
    var telephone = that.data.telephone;
    var password = that.data.password;
    var code = that.data.code;
    if (!telephone || !code || !password) {
      wx.showToast({ title: '各项信息必填', icon: 'none' });
      return false;
    } else if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none'
      });
      return false;
    } else if (password.length < 6) {
      wx.showToast({
        title: '密码长度不得少于6位！',
        icon: 'none',
        duration: 1000
      });
      return false;
    } else {
      wx.showLoading({ title: '提交中', mask: true });
      wx.request({
        method: 'POST',
        url: app.preUrl + 'toVerifyCode',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded;'
        },
        data: {
          userType: 1,
          telephone: telephone,
          code: code
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.code == 100) {
            let data = {
              telephone: telephone,
              code: code,
              password: password
            };
            wx.navigateTo({
              url: 'register?data=' + JSON.stringify(data)
            });
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            });
          }
        },
        fail: function(error) {}
      });
    }
  },
  onUnload() {
    //页面卸载  当redirectTo或navigateBack的时候调用
    clearInterval(this.data.timer);
  }
});
